//
//  MessageTableViewCell.m
//  tuanGG
//
//  Created by 微他 on 15/12/3.
//  Copyright © 2015年 weita. All rights reserved.
//

#import "MessageTableViewCell.h"

@implementation MessageTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth-20, 30)];
        self.timeLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.timeLabel];

        self.selectedImage = [[UIImageView alloc]initWithFrame:CGRectMake(-23, (CGRectGetHeight(self.frame))/2, 23, 23)];
        [self addSubview:self.selectedImage];

        self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.timeLabel.frame), ScreenWidth-20, 30)];
        self.contentLabel.font = [UIFont systemFontOfSize:14];
        self.contentLabel.numberOfLines = 0;
        [self.contentView addSubview:self.contentLabel];
    }
    return self;
}
- (void)awakeFromNib {
    // Initialization code
}


- (void) setCheckImageViewCenter:(CGPoint)pt alpha:(CGFloat)alpha animated:(BOOL)animated
{
    if (animated)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.3];

        m_checkImageView.center = pt;
        m_checkImageView.alpha = alpha;

        [UIView commitAnimations];
    }
    else
    {
        m_checkImageView.center = pt;
        m_checkImageView.alpha = alpha;
    }
}
- (void) setEditing:(BOOL)editting animated:(BOOL)animated
{
    if (self.editing == editting)
    {
        return;
    }

    [super setEditing:editting animated:animated];

    if (editting)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        if (m_checkImageView == nil)
        {
            m_checkImageView = [[UIImageView alloc] initWithImage:IMGNAME(@"未选中状态")];
            m_checkImageView.bounds = CGRectMake(0, 0, 22, 22);
            [self addSubview:m_checkImageView];
        }

        [self setChecked:m_checked];
        m_checkImageView.center = CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
                                              CGRectGetHeight(self.bounds) * 0.5);
        m_checkImageView.alpha = 0.0;
        [self setCheckImageViewCenter:CGPointMake(20.5, CGRectGetHeight(self.bounds) * 0.5)
                                alpha:1.0 animated:animated];
    }
    else
    {
        m_checked = NO;
        //		self.selectionStyle = UITableViewCellSelectionStyleBlue;
        self.backgroundView = nil;

        if (m_checkImageView)
        {
            [self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
                                                      CGRectGetHeight(self.bounds) * 0.5)
                                    alpha:0.0
                                 animated:animated];
        }
    }
}

- (void) setChecked:(BOOL)checked
{
    if (checked)
    {
        [m_checkImageView sd_setImageWithURL:nil placeholderImage:IMGNAME(@"选中状态")];
        self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
    }
    else
    {
        [m_checkImageView sd_setImageWithURL:nil placeholderImage:IMGNAME(@"未选中状态")];
        self.backgroundView.backgroundColor = [UIColor whiteColor];
    }
    m_checked = checked;
}
- (void)dealloc
{
    m_checkImageView = nil;
}
@end
